<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, FormInstance, FormRules } from 'element-plus';
import useStore from '@/store';
import { props } from './form';
defineOptions({ name: 'ActionLogForm' });
const _props = defineProps(props);
const save = async () => {
  if (!ruleFormRef.value) return;
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      var rows = tableRef.value.getSelectionRows();
      if (rows.length <= 0) {
        ElMessage.success('请选择镜种');
        return;
      }
      form.lensIds = [];
      for (let i = 0; i < rows.length; i++) {
        var row = rows[i];
        form.lensIds.push(row.id);
      }
      window.$api.stock.save(form).then(res => {
        ElMessage.success('保存成功!');
        useStore.window.windowRemove(_props.window?.name);
        if (_props.window?.callBack) _props.window?.callBack();
      });
    }
  });
};
const tableRef = ref(null);
const lensCode = ref(null);
const warehouses = ref(null);
const lenses = ref(null);
const loading = ref(false);
const ruleFormRef = ref<FormInstance>();
const form = reactive({
  warehouseId: null,
  lensIds: [],
});
const rules = reactive<FormRules>({
  warehouseId: [{ required: true, message: '请选择仓库', trigger: 'change' }],
});
/** 加载基础数据,字典等 */
const init = async () => {
  await window.$api.warehouse.getWarehouses().then(res => {
    warehouses.value = res.data;
  });
};

const warehouseChange = val => {
  form.warehouseId = val;
  getLenses();
};

const getLenses = () => {
  var params = { warehouseId: form.warehouseId, factoryLensCode: lensCode.value };
  loading.value = true;
  window.$api.lens.getCanAddStockLenses(params).then(res => {
    lenses.value = res.data;
    loading.value = false;
  });
};
const lensCodeEnter = () => {
  getLenses();
};

onMounted(() => {
  init();
  // if (_props.window.params) {
  //   if (_props.window.params.id) {
  //     detail();
  //   }
  // }
});
</script>

<template>
  <windows-window :window="window">
    <template #button>
      <windows-button-group size="18">
        <windows-button icon="fluent-save-28-regular" @click="save"></windows-button>
      </windows-button-group>
    </template>
    <el-form ref="ruleFormRef" :model="form" :rules="rules" label-width="70px" class="demo-ruleForm" size="default" status-icon>
      <el-row>
        <el-col :span="24">
          <el-form-item label="仓库" prop="warehouseId">
            <el-select clearable filterable v-model="form.warehouseId" @change="warehouseChange">
              <el-option v-for="item in warehouses" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-table ref="tableRef" :data="lenses" style="width: 100%" height="300" highlight-current-row v-loading="loading">
            <el-table-column type="selection" width="55" />
            <el-table-column type="index" width="80" label="序号" align="center" />
            <el-table-column prop="factoryLensCode" label="镜种代码" align="center" minWidth="200" show-overflow-tooltip>
              <template #header>
                <el-input v-model="lensCode" size="small" placeholder="镜种代码" @keyup.enter.native="lensCodeEnter" />
              </template>
              <template #default="scope">
                <span>{{ scope.row.factoryLensCode }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="factoryLensName" label="镜种名称" align="center" minWidth="200" show-overflow-tooltip></el-table-column>
            <el-table-column prop="maxSPH" label="最大SPH" align="center" minWidth="100"></el-table-column>
            <el-table-column prop="minSPH" label="最小SPH" align="center" minWidth="100"></el-table-column>
            <el-table-column prop="isBulk" label="是否ADD镜种" align="center" width="150">
              <template #default="scope">
                <el-switch v-model="scope.row.isBulk" inline-prompt active-text="是" inactive-text="否" disabled></el-switch>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
    </el-form>
  </windows-window>
</template>

<style scoped lang="scss">
#edit {
  height: 100%;
}
.el-form {
  margin: 30px 20px;
}
.custom-node {
  display: flex;
  align-items: center;
  span {
    margin-left: 5px;
  }
}
.select-icon {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
  margin-top: 5px;
  background: #fff;
  width: 100%;
  height: 200px;
  overflow: auto;
  .el-image {
    margin: 10px;
    cursor: pointer;
    border-radius: 5px;
    padding: 5px;
    &:hover,
    &.active {
      background: #dcdfe6;
    }
  }
}
</style>
